using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Hosting;
using System.IO;
using System.Reflection;
using System.Collections;

namespace SeguradoraIA.Framework
{
    public class AssemblyResourceProvider : System.Web.Hosting.VirtualPathProvider
    {
        public AssemblyResourceProvider() { }
        private bool IsAppResourcePath(string virtualPath)
        {
            String checkPath =
               VirtualPathUtility.ToAppRelative(virtualPath);
            return ((checkPath.IndexOf("App_Resource/") > -1) || (checkPath.IndexOf("App_ResourceEx/") > -1));
        }
        public override bool FileExists(string virtualPath)
        {
            return (IsAppResourcePath(virtualPath) || base.FileExists(virtualPath));
        }
        public override VirtualFile GetFile(string virtualPath)
        {
            if (IsAppResourcePath(virtualPath))
                return new AssemblyResourceVirtualFile(virtualPath);
            else
                return base.GetFile(virtualPath);
        }
        public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
        {
            if (virtualPathDependencies != null &&
                virtualPathDependencies is ArrayList &&
                ((ArrayList)virtualPathDependencies).Count > 0)
            {
                foreach (string item in virtualPathDependencies)
                {
                    if (IsAppResourcePath(item))
                        return null;
                }
                return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
            }
            else if (IsAppResourcePath(virtualPath))
                return null;
            else
                return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }
    }

    class AssemblyResourceVirtualFile : VirtualFile
    {
        string path;
        public AssemblyResourceVirtualFile(string virtualPath)
            : base(virtualPath)
        {
            if (virtualPath.IndexOf("App_ResourceEx/") > -1)
            {
                path = virtualPath;
            }
            else
                path = VirtualPathUtility.ToAppRelative(virtualPath);
        }
        public override System.IO.Stream Open()
        {
            string[] parts = path.Split('/');
            string assemblyName = string.Empty;
            string resourceName = string.Empty;
            foreach (string str in parts)
            {
                if (str.IndexOf(".dll") > 0)
                    assemblyName = str;
                if (str.IndexOf(".ascx") > 0 ||
                    str.IndexOf(".aspx") > 0 ||
                    str.IndexOf(".Master") > 0)
                    resourceName = str;
            }

            if (!string.IsNullOrEmpty(assemblyName))
            {
                assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
                System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(assemblyName);
                if (assembly != null && !string.IsNullOrEmpty(resourceName))
                {
                    return assembly.GetManifestResourceStream(resourceName);
                }
            }
            return null;
        }
    }
}
